package com.hongxun.budget.ui.activity.ys;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;

import com.hongxun.budget.R;
import com.hongxun.budget.greendao.EMaterialOldEntityDao;
import com.hongxun.budget.ui.activity.BaseListActivity;
import com.hongxun.budget.ui.dialog.MaterialInputDialog;
import com.hongxun.budget.utils.GreenDaoManager;
import com.hongxun.budget.utils.SPUtils;
import com.hongxun.budget.utils.T;
import com.hongxun.greenentity.tk.BMaterialEntity;
import com.hongxun.greenentity.ys.EMaterialOldEntity;
import com.hongxun.interfaces.ResponseListener;
import com.jessewu.library.SuperAdapter;
import com.jessewu.library.view.ViewHolder;

import org.greenrobot.greendao.query.QueryBuilder;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class YsUserOldMaterialActivity extends BaseListActivity {

    List<EMaterialOldEntity> datas = new ArrayList<>();
    SuperAdapter<EMaterialOldEntity> adapter;
    @BindView(R.id.search_info)
    EditText searchInfo;

    @Override
    public int getMyListLayoutId() {
        return R.layout.activity_user_old_material;
    }

    @Override
    public SuperAdapter getAdapter() {
        return adapter = new SuperAdapter<EMaterialOldEntity>(R.layout.content_useold_material) {
            @Override
            public void bindView(ViewHolder viewHolder, final EMaterialOldEntity d, final int i) {
                ((TextView) viewHolder.getView(R.id.device_type)).setText(d.getMBMidtype());
                ((TextView) viewHolder.getView(R.id.name)).setText(d.getMBName());
                ((TextView) viewHolder.getView(R.id.parameter)).setText(d.getMBParameter());
                ((TextView) viewHolder.getView(R.id.count)).setText(d.getDRWhere() + "");
                ((TextView) viewHolder.getView(R.id.unit)).setText("(" + d.getMBUnit() + ")");
            }
        };
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        adapter.setOnItemClickListener(new SuperAdapter.OnItemClickListener<EMaterialOldEntity>() {
            @Override
            public void onClick(int i, final EMaterialOldEntity d) {
                BMaterialEntity b = new BMaterialEntity();
                b.setGuid(d.getGuid());
                b.setMBigtype(d.getMBBigtype());
                b.setMMidtype(d.getMBMidtype());
                b.setMParameter(d.getMBParameter());
                b.setmCount(d.getMBCount());
                b.setMLabel(d.getDLabel());
                b.setMPrice(0.0);
                b.setMUnit(d.getMBUnit());
                b.setMName("(利旧)" + d.getMBName());
                new MaterialInputDialog().setB(b).setListener(new ResponseListener() {
                    @Override
                    public void response(Object... data) {
                        BMaterialEntity m = (BMaterialEntity) data[0];
                        if (m.getmCount() > Double.parseDouble(d.getDRWhere())) {
                            T.showLong(context, "利旧数量不能大于旧材料数量");
                            return;
                        }
                        List<BMaterialEntity> result = new ArrayList<BMaterialEntity>();
                        result.add(m);
                        Intent intent = new Intent();
                        intent.putExtra("material", (Serializable) result);
                        setResult(Activity.RESULT_OK, intent);
                        finish();
                    }
                }).show(getFragmentManager(), "");
            }
        });
        adapter.setData(datas);
        adapter.setEmptyDataView(R.layout.view_empty);
        getData();
    }

    void getData() {
        String search = searchInfo.getText().toString();
        QueryBuilder<EMaterialOldEntity> builder = GreenDaoManager.getInstance().getmDaoSession().getEMaterialOldEntityDao().queryBuilder();
        List<EMaterialOldEntity> list = builder.where(EMaterialOldEntityDao.Properties.MBDatastate.notEq("删除")
                , EMaterialOldEntityDao.Properties.MBWork.eq("拆除")
                , EMaterialOldEntityDao.Properties.DROpinion.eq("10")
                , EMaterialOldEntityDao.Properties.DRWhere.gt(0)
                , EMaterialOldEntityDao.Properties.PGuid.eq(SPUtils.get(context, "project_guid", "")),
                builder.or(EMaterialOldEntityDao.Properties.MBName.like("%" + search + "%")
                        , EMaterialOldEntityDao.Properties.MBParameter.like("%" + search + "%"))).list();
        List<EMaterialOldEntity> deletes = new ArrayList<>();
        for (EMaterialOldEntity d : list) {
            if (Double.parseDouble(d.getDRWhere()) <= 0) {
                deletes.add(d);
            }
        }
        list.removeAll(deletes);
        datas.addAll(list);
        adapter.notifyDataSetChanged();
    }

    @OnClick(R.id.search_button)
    public void onViewClicked() {
        getData();
    }
}
